#include "mainwindow.h"
#include<QPainter>
#include<win.h>
#include<input.h>
#include<QDialog>
#include<QButtonGroup>
#include<draw.h>

extern int hang,lie;
MainWindow::MainWindow(QWidget *parent)
  : QWidget(parent)
{
   input p;
   p.exec();
   this->resize(QSize(200+80*lie,160+80*hang ));//横宽、纵宽
   reset();
   create_button();
}


MainWindow::~MainWindow()
{

}


void MainWindow::create_button()
{
      m_button = new QButtonGroup(this);
      QVector<QPushButton*> n_button;
     for(int i=0;i<lie;i++)
      {
         QPushButton *button=new QPushButton;
         button->setText("↓");
         button->resize(80,80);
         button->move(100+80*i,20);
         n_button.push_back(button);
         m_button->addButton(n_button[i],i);
         button->setParent(this);
         button->show();
      }
    QObject::connect(m_button, SIGNAL(buttonClicked(int)), this, SLOT(handleButton(int)));
}


void MainWindow::paintEvent(QPaintEvent *)
{
    //棋盘绘制部分
   QPainter Painter;
   Painter.begin(this);
   Painter.setPen(Qt::black);
   Painter.setBrush(Qt::white);
    for(int i=100;i<100+80*lie;i+=80)
      for(int j=100;j<100+80*hang;j+=80)
          Painter.drawRect(i,j,80,80);//横坐标、纵坐标、长、宽

    //棋子绘制部分
    for(int i=0;i<hang;i++)
        for(int j=0;j<lie;j++)
        {
            if(chess[i][j]==1)
             {
                Painter.setPen(Qt::yellow);
                Painter.setBrush(Qt::blue);
                Painter.drawRect(100+j*80,100+i*80,80,80);
             }
            if(chess[i][j]==2)
            {
                Painter.setPen(Qt::yellow);
                Painter.setBrush(Qt::red);
                Painter.drawRect(100+j*80,100+i*80,80,80);
            }
        }
    Painter.end();
}


void MainWindow::handleButton(int i)//按钮对应的槽函数
{
     if(player==1&&full[i]==0)
      {
          chess[col[i]][i]=1;
           if(judge(chess,col[i],i)==1)
          {
            win_blue=new win(this);
            win_blue->show();
          }
          if(col[i]>=1)
           col[i]--;
          else
              full[i]=1;
      }
     else if(player==2&&full[i]==0)
      {
           chess[col[i]][i]=2;
           if(judge(chess,col[i],i)==2)
          {
            win_red=new red(this);
            win_red->show();
          }
         if(col[i]>=1)
          col[i]--;
         else
             full[i]=1;
      }
     if(judge(chess,col[i],i)==3)
     {
         draw=new Draw(this);
         draw->show();
     }
       if(player==1)
           player=2;
       else
           player=1;
              update();
}


void MainWindow::reset(){
    //棋盘生成函数
   chess.resize(hang);
   for(int i=0;i<hang;i++)
       chess[i].resize(lie);

   //棋盘初始化函数
   int set=-1;
    for(int i=0;i<hang;i++)
        for(int j=0;j<lie;j++)
            chess[i][j]=--set;

    col=new int[lie];
    for(int i=0;i<lie;i++)
        col[i]=hang-1;

    full=new bool[lie];
      for(int i=0;i<lie;i++)
            full[i]=0;
}


int MainWindow::judge(QVector<QVector<int>>,int x,int y)
{
    //判断横行
        for(int i=0;i<lie-3;i++)
           {
                 if(chess[x][i]==1&&chess[x][i+1]==1&&chess[x][i+2]==1&&chess[x][i+3]==1)
                     return 1;
                 if(chess[x][i]==2&&chess[x][i+1]==2&&chess[x][i+2]==2&&chess[x][i+3]==2)
                     return 2;
           }
    //判断竖行
     for(int i=0;i<hang-3;i++)
     {
             if(chess[i][y]==1&&chess[i+1][y]==1&&chess[i+2][y]==1&&chess[i+3][y]==1)
                 return 1;
             if(chess[i][y]==2&&chess[i+1][y]==2&&chess[i+2][y]==2&&chess[i+3][y]==2)
                 return 2;
     }
    //判断斜线
     int record_x=x,record_y=y;
   for(;x>0&&y>0;x--,y--);
      for(;x+3<hang&&y+3<lie;x++,y++)
         {
          if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
              return 1;
           if(chess[x][y]==2&&chess[x+1][y+1]==2&&chess[x+2][y+2]==2&&chess[x+3][y+3]==2)
              return 2;
         }
      x=record_x,y=record_y;
      for(;x>0&&y<lie-1;x--,y++);
          for(;x+3<hang&&y-3>=0;x++,y--)
            {
              if(chess[x][y]==1&&chess[x+1][y-1]==1&&chess[x+2][y-2]==1&&chess[x+3][y-3]==1)
                  return 1;
              if(chess[x][y]==2&&chess[x+1][y-1]==2&&chess[x+2][y-2]==2&&chess[x+3][y-3]==2)
                  return 2;
            }
     //判断棋盘已满
         for(int i=0;full[i]==1&&i<lie;i++)
             if(i==lie-1)
                 return 3;

     //均不满足胜利条件
       return 0;
}

